package com.helloandroid.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.view.marginLeft
import com.helloandroid.R
import com.helloandroid.tools.BitmapMemCache
import com.helloandroid.tools.MyLog
import kotlin.math.min



class XView @JvmOverloads constructor(
        ctx: Context,
        attrs: AttributeSet? = null,
        defStyle: Int = 0
) : View(ctx, attrs, defStyle) {
    private val beiziBitmap: Bitmap = BitmapMemCache.getBitmap(R.drawable.dd_heshui_beizi)
    private val shuiBitmap: Bitmap = BitmapMemCache.getBitmap(R.drawable.dd_heshui_shui)

    var drawHeight = 0f

    private val imgWidth:Int
    val imgHeight:Int

    private var dstRect : Rect
    var clipRect: RectF

    init {
        MyLog.elog("beizi==> width:${beiziBitmap.width},height:${beiziBitmap.height}")
        MyLog.elog("shuiBitmap==> width:${shuiBitmap.width},height:${shuiBitmap.height}")
        imgWidth = beiziBitmap.width
        imgHeight = beiziBitmap.height
        dstRect = Rect(0,0,imgWidth,imgHeight)
        clipRect = RectF()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        var newWidth = 0
        var newHeight = 0

        // width
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        if (widthMode == MeasureSpec.EXACTLY){
            newWidth = MeasureSpec.getSize(widthMeasureSpec)
        }else{
            newWidth = imgWidth
            if (widthMode == MeasureSpec.AT_MOST){
                newWidth = min(MeasureSpec.getSize(widthMeasureSpec),newWidth)
            }
        }

        // height
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        if (heightMode == MeasureSpec.EXACTLY){
            newHeight = MeasureSpec.getSize(heightMeasureSpec)
        }else{
            newHeight = imgHeight
            if (heightMode == MeasureSpec.AT_MOST){
                newHeight = min(MeasureSpec.getSize(heightMeasureSpec),newHeight)
            }
        }
        setMeasuredDimension(newWidth,newHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas?.let {

            val left = (width - imgWidth) / 2

            dstRect.set(marginLeft + left,0,imgWidth + marginLeft + left,height)
            it.drawBitmap(beiziBitmap,null ,dstRect,null)
            it.save()


            when(state){
                State.AlreadyDaKa-> {
                    // 空的 啥也不干
                }
                State.NotDaka->{
                    // 还没有打卡或者打卡时间还没到,满的
                    clipRect.set(marginLeft.toFloat(), 0f,(width + marginLeft).toFloat(),height.toFloat())
                    it.clipRect(clipRect)
                    it.drawBitmap(shuiBitmap,null,dstRect,null)
                }
                State.DakaIng->{
                    clipRect.set(marginLeft.toFloat(),height - drawHeight ,(width + marginLeft).toFloat(),height.toFloat())
                    it.clipRect(clipRect)
                    it.drawBitmap(shuiBitmap,null,dstRect,null)
                }
            }

//            if (state == State.AlreadyDaKa){
//                // 杯子是空的
//            }else{
//                // 杯子不是空的
//                if (drawHeight > 0){
//                    clipRect.set(marginLeft.toFloat(),height - drawHeight ,(width + marginLeft).toFloat(),height.toFloat())
//                    it.clipRect(clipRect)
//                    it.drawBitmap(shuiBitmap,null,dstRect,null)
//                }else{
//                    clipRect.set(marginLeft.toFloat(), 0f,(width + marginLeft).toFloat(),height.toFloat())
//                    it.clipRect(clipRect)
//                    it.drawBitmap(shuiBitmap,null,dstRect,null)
//                }
//            }

            it.restore()
        }
    }

    // 只要打卡了,杯子就是空的
    var state = State.NotDaka
        set(value) {
            field = value
            drawHeight = 0f
            invalidate()
        }

    enum class State{
        AlreadyDaKa, // 已打卡
        NotDaka, // 还没打卡
        DakaIng, // 打卡中
    }
}